package com.example.wishstar.viewmodel;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.ObservableField;

import com.example.wishstar.MyApplication;
import com.example.wishstar.model.Users;
import com.example.wishstar.repository.UpdateUserMsgRepository;
import com.example.wishstar.utils.ApiResponse;
import com.example.wishstar.view.fragment.UsersFragment;

import retrofit2.Response;

public class UpdateUserMsgViewModel implements UpdateUserMsgRepository.UpdateUserMsgCallBack {
    private ObservableField<Users> updateUserMsgObservableField;
    private Context context;
    private UpdateUserMsgRepository updateUserMsgRepository;
    int userId = MyApplication.userId;

    public UpdateUserMsgViewModel(Context context){
        this.context = context;
        Users updateUserMsg = new Users(0, "", "");
        updateUserMsgObservableField = new ObservableField<>();
        updateUserMsgObservableField.set(updateUserMsg);

        updateUserMsgRepository = new UpdateUserMsgRepository(MyApplication.getApi());
    }

    public String getNickname() {
        return updateUserMsgObservableField.get().nickname;
    }

    public void setNickname(String nickname) {
        updateUserMsgObservableField.get().nickname = nickname;
    }

    public String getMotto() {
        return updateUserMsgObservableField.get().motto;
    }

    public void setMotto(String motto) {
        updateUserMsgObservableField.get().motto = motto;
    }

    public void updateUserMsg(View view) {
        updateUserMsgRepository.updateUserMsg(userId, getNickname(), getMotto(), this);
    }

    @Override
    public void callback(Response<ApiResponse<String>> response) {
        Toast.makeText(context, response.body().getMessage(), Toast.LENGTH_SHORT).show();
        if(response.body().code == 200) {
            Intent intent = new Intent(context, UsersFragment.class);
            context.startActivity(intent);
        }
    }

}
